﻿using System;
using System.Linq;
using System.Linq.Dynamic;
using System.Web.Mvc;
using ChoViet.CMS.Properties;
using ChoViet.CMS.ViewModels;
using ChoViet.Lib.Common;
using ChoViet.Lib.Paging;
using ChoViet.Model;

namespace ChoViet.CMS.Controllers
{
    public class CommentController : BaseController
    {
        ChoVietDataContext eDataContext = new ChoVietDataContext();

        #region CMS: Comments
        public ActionResult IndexCms(int? page)
        {
            try
            {
                if (!page.HasValue) page = 1;
                string order = UserState.getUserStateFromRequest("Comment.order", "order", "ID").ToString();
                string orderDir = UserState.getUserStateFromRequest("Comment.orderDir", "orderDir", "desc").ToString();
                int currentPub = Convert.ToInt32(UserState.getUserStateFromRequest("Comment.TypePubYN", "TypePubYN", 0));
                string keyword = UserState.getUserStateFromRequest("Comment.Keyword", "Keyword", "").ToString();
                int pageSize = int.Parse(UserState.getUserStateFromRequest("Comment.pageSize", "PageSize", 20).ToString());

                var pubYNList = Utils.StringToDictionary(Settings.Default.CommentPubYN.ToString(), ",");
                var CommentList = eDataContext.Comments
                   .Where(cm => currentPub == 1 ? cm.PublishYN == true : true)
                   .Where(cm => currentPub == 2 ? (cm.PublishYN == false || cm.PublishYN == null) : true)
                   .Where(p => (string.IsNullOrEmpty(keyword) ? true :
                                (p.ID.ToString().ToLower().Contains(keyword.ToLower().Trim())
                                )|| (p.AdID.ToString().ToLower().Contains(keyword.ToLower().Trim()))||
                                (p.Title.ToLower().Contains(keyword.ToLower().Trim()))
                                ))
                                .OrderBy(string.Format("{0} {1}", order, orderDir))
                                .ToPagedList(page.Value - 1, pageSize);

                ViewData["orderDir"] = orderDir;
                ViewData["order"] = order;

                return View(new CommentCmsViewModels
                {
                    ListComment = CommentList,
                    CurrentPubYN = currentPub,
                    ListPubYN = pubYNList,
                    Keyword = keyword
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ActionResult DetailsCms(int id)
        {
            var param = eDataContext.Comments.FirstOrDefault(e => e.ID == id);
            return View(param);
        }
        #endregion
    }
}
